<script setup>
import { nanoid } from "nanoid";
</script>

<template>
  <div>
    <h1>人员列表</h1>
    <div>
      <input type="text" v-model.trim="name" />
      <button @click="add">添加</button>
    </div>
    <ul>
      <li v-for="p in personList" :key="p.id">{{ p.name }}</li>
    </ul>
    <div>求和结果：{{ sum }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
    };
  },
  computed: {
    personList() {
      return this.$store.state.personList;
    },
    sum() {
      return this.$store.state.sum;
    },
  },
  methods: {
    add() {
      if (!this.name) {
        alert("请输入姓名");
        return;
      }
      this.$store.commit("AddPerson", { id: nanoid(), name: this.name });
      this.name = "";
    },
  },
};
</script>

<style></style>
